package com.study.dao.impl;


import com.study.bean.Blog;
import com.study.bean.BlogQuery;
import com.study.dao.IBlogDao;
import com.study.util.JdbcUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDaoImpl implements IBlogDao {
    @Override
    public List<Blog> selectAll() throws SQLException {
        String allsql = "select * from blog";

        QueryRunner queryRunner = new QueryRunner();
        List<Blog> query = queryRunner.query(JdbcUtil.getConnection(), allsql, new BeanListHandler<>(Blog.class));
        return query;
    }

    @Override
    public List<Blog> selectByPage(int offset, Integer limit) throws SQLException {
        String sql = "select id,name,address,time from blog limit ?,?";
        Object[] objects = {offset, limit};
        QueryRunner queryRunner = new QueryRunner();
        List<Blog> query = queryRunner.query(JdbcUtil.getConnection(), sql,
                new BeanListHandler<>(Blog.class), objects);
        return query;
    }

    @Override
    public List<Blog> selectByPage(BlogQuery blogQuery) throws SQLException {
        System.out.println("DAO层");
        String sql = "select * from blog where 1=1";
        //构造一个集合存放所有的条件
        List<Object> queryList = new ArrayList<>();
        String queryName = blogQuery.getName();
        String content = blogQuery.getContent();
        if (queryName != null && !queryName.equals("")) {
            sql += " and name like ?";
            queryList.add("%" + queryName + "%");
        }
        if (content != null && !content.equals("")) {
            sql += " and `content` like ?";
            queryList.add("%" + content + "%");
        }
        sql += " limit ?,?";

        Integer offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
        queryList.add(offset); //开始的位置
        queryList.add(blogQuery.getLimit());   //没页限制多少条

        QueryRunner queryRunner = new QueryRunner();
        List<Blog> query = queryRunner.query(JdbcUtil.getConnection(), sql, new BeanListHandler<>(Blog.class), queryList.toArray());

        return query;
    }

    @Override
    public int selectTotalCount() throws SQLException {

        String sql = "select count(*) from blog";

        QueryRunner queryRunner = new QueryRunner();

        Long count = queryRunner.query(JdbcUtil.getConnection(), sql, new ScalarHandler<Long>());

        return count != null ? count.intValue() : 0; // Convert Long to int and handle null
    }

    @Override
    public int selectTotalCount(BlogQuery blogQuery) throws SQLException {

        String sql = "select count(*) from blog where 1=1";
        //构造一个集合存放所有的条件
        List<Object> queryList = new ArrayList<>();
        String queryName = blogQuery.getName();
        String content = blogQuery.getContent();
        if (queryName != null && !queryName.equals("")) {
            sql += " and name like ?";
            queryList.add("%" + queryName + "%");
        }
        if (content != null && !content.equals("")) {
            sql += " and content like ?";
            queryList.add("%" + content + "%");
        }
        QueryRunner queryRunner = new QueryRunner();

        if (queryList.size() == 0) {
            Long count = queryRunner.query(JdbcUtil.getConnection(), sql, new ScalarHandler<Long>());
            return count != null ? count.intValue() : 0;
        } else {
            Long count = queryRunner.query(JdbcUtil.getConnection(), sql, new ScalarHandler<Long>(), queryList.toArray());
            return count != null ? count.intValue() : 0;
        }


    }

    @Override
    public void deleteById(int id) throws SQLException {

        String sql = "delete from blog where id=?";

        QueryRunner queryRunner = new QueryRunner();

        int update = queryRunner.update(JdbcUtil.getConnection(), sql, id);

    }

    @Override
    public void add(Blog blog) throws SQLException {

        String sql = "insert into blog(name,content,image) values(?,?,?)";


        QueryRunner queryRunner = new QueryRunner();
        Object[] objects = {blog.getName(), blog.getContent(), blog.getImage()};
        int update = queryRunner.update(JdbcUtil.getConnection(), sql, objects);

        System.out.println("count: " + update);

    }

    @Override
    public Blog selectById(int id) throws SQLException {


        String sql = "select * from blog where id=?";

        QueryRunner queryRunner = new QueryRunner();

        Blog query = queryRunner.query(JdbcUtil.getConnection(), sql, new BeanHandler<>(Blog.class), id);

        return query;
    }

    @Override
    public void update(Blog blog) throws SQLException {

        String sql = "update blog set name=?,content=?  where id=?";
        Object[] objects = {blog.getName(), blog.getContent(), blog.getId()};

        QueryRunner queryRunner = new QueryRunner();
        int update = queryRunner.update(JdbcUtil.getConnection(), sql, objects);
        System.out.println("更新" + update + "行");
    }
}
